<!--
 * @Author: luosi 1297957337@qq.com
 * @Date: 2024-11-28 10:34:39
 * @LastEditors: huang liang miao 1127255342@qq.com
 * @LastEditTime: 2024-12-24 14:35:02
 * @FilePath: \sr_system_manager_ui\src\views\achievementReward\assessmentReward\researchAssessment\components\PersonSummary.vue
 * @Description: 人员汇总统计列表
-->
<template>
  <div class="person-summary-container">
    <div class="search" v-if="showSearch">
      <SearchBar ref="searchBarRef" :components="conditionComponents" :hideRightArea="false" @search="refreshTableData" />
    </div>
    <div class="list">
      <TableForm ref="tableRef" :data="dataList" :columns="columns" :tableLoading="loading" :total-count="total"
        v-model:showSearch="showSearch" :showRowSelection="true" :showPagination="true" :showToolBar="true"
        :download="(params) => { exportListData(params)}"
        @refresh="refreshTableData">
        <template #achvInfoResultVOS="{ record }">
          <div v-if="record?.achvInfoResultVOS?.length">
            <div v-for="item in record.achvInfoResultVOS" class="other-tr">
                <span>{{ achvTypeMap[item.achvType as keyof typeof achvTypeMap] }}：</span>
                <span>
                  {{ item.resultDetailVOS?.map(ele =>
                    `${ele.quanIndicators}${ele.number}${ele.unit}${ props.rewardFlag ? ele.rewardScore :ele.researchScore}分`)?.join('、') }}
                </span>
              </div>
          </div>
          <div v-else>-</div>
        </template>
        <template #default="{ record }">
          <el-button link type="primary" @click="handleDetail(record.id)">查看</el-button>
        </template>
      </TableForm>
    </div>
  </div>
</template>

<script setup lang="ts">
import { achvTypeMap } from '@/utils/constText'
import { achvCommonCheckResultList, exportResultListApi } from '@/api/resultsManagement';
import { AchvCheckResulVO, PageBeanAchvCheckResulVO } from '@/api/resultsManagement/types';
import { dictEnum } from '@/utils/constEnum';
import { downloadFile } from '@/utils/download'

const props = defineProps({
  year: {
    type: Number
  },
  rewardFlag: { // 是否奖励标志
    type: Boolean
  }
})

const { proxy } = getCurrentInstance()

const dict = proxy.useDict(dictEnum.department, dictEnum.jobTitle)

const router = useRouter()
const searchBarRef = ref()
const showSearch = ref(true)
// 请求参数
const parameters = ref({
  page: 1,
  size: 20,
  departmentIds: '', // 学院（部）
  titleId: '',
  name: undefined, // 工号/姓名
})

// 导出参数
const downloadParameters = ref({
  statisticalYear: props.year,
  status: 5,
  researchScoreFlag: true,
  rewardFlag: props.rewardFlag,
  idList: []
})

const conditionComponents = computed(() => [
  {
    type: 1,
    key: 'departmentIds',
    placeholder: '学院（部）',
    config: {
      multiple: true,
      options: dict[dictEnum.department]
    }
  },
  {
    type: 1,
    key: 'titleId',
    placeholder: '职称',
    config: {
      options: dict[dictEnum.jobTitle]
    }
  },
  {
    type: 0,
    key: 'name',
    placeholder: '请输入姓名',
  }
])

const tableRef = ref()
const total = ref(0)
const dataList = ref<AchvCheckResulVO[]>([])
const columns = [
  {
    title: '序号',
    width: 48,
    dataIndex: 'index',
    formatter: (row: any, column: any, cellValue: any, index: number) => {
      return index + 1
    }
  },
  {
    title: '所属院校（部）',
    minWidth: 100,
    dataIndex: 'departmentName',
  },
  {
    title: '工号',
    minWidth: 60,
    dataIndex: 'code',
  },
  {
    title: '姓名',
    minWidth: 60,
    dataIndex: 'name',
  },
  {
    title: '职称',
    minWidth: 100,
    dataIndex: 'title',
  },
  {
    title: props.rewardFlag ? '总奖励分' : '总科研分',
    minWidth: 100,
    dataIndex: props.rewardFlag ? 'rewardTotalScore' : 'researchTotalScore',
  },
  {
    title: '工作量明细',
    minWidth: 200,
    dataIndex: 'achvInfoResultVOS',
    slotName: 'achvInfoResultVOS'
  },
  {
    title: '操作',
    width: 100,
    dataIndex: 'default',
    slotName: 'default',
  }
]

const { run: getData, loading } = useRequest(achvCommonCheckResultList, {
  manual: true,
  onSuccess: (res: PageBeanAchvCheckResulVO) => {
    const { content, totalElements } = res || {}
    dataList.value = content || []
    total.value = totalElements || 0
  }
})

/* 导出列表数据 */
const { runAsync: exportFile } = useRequest(exportResultListApi, {
  // false 为自动执行
  manual: true,
  // 请求成功
  onSuccess: () => {
  },
  // 请求失败
  onError: () => {
  },
})
// 导出
const exportListData = async (params) => {
  downloadParameters.value.idList = params
  const res = await exportFile({...downloadParameters.value, ...searchBarRef.value.params})
  downloadFile(res)
}

const refreshTableData = (params) => {
  parameters.value = { ...parameters.value, ...params }
  getData({
    ...parameters.value,
    statisticalYear: props.year,
    rewardFlag: props.rewardFlag,
    departmentIds: parameters.value.departmentIds,
    titleId: parameters.value.titleId,
    name: parameters.value.name
  })
}

const handleDetail = (id: string) => {
  if (props.rewardFlag) {
    router.push({ path: '/achievementReward/assessmentReward/researchReward/rewardDetail', query: { year: props.year, rewardFlag: props.rewardFlag, id } })
  } else {
    router.push({ path: '/achievementReward/assessmentReward/researchAssessment/personalPointDetail', query: { year: props.year, rewardFlag: props.rewardFlag, id } })
  }
}

onMounted(() => {
  tableRef.value?.refreshInitPage()
})
</script>

<style scoped lang="scss">
.person-summary-container {
  --at-apply: flex flex-col space-y-[16px] h-full;

  .search {
    --at-apply: px-[20px] py-[16px] bg-white rounded-[4px];
  }

  .list {
    --at-apply: flex-1 px-[20px] pb-[20px] bg-white rounded-[4px];
  }
  :deep(.cell:has(.other-tr)){
    padding: 0 !important;
    .other-tr{
      @apply: px-[8px] py-[14px] border-b border-color-[#BEDAFF];
      &:last-child{
        @apply: border-none;
      }
    }
  }
}
</style>